<?php
class Controller_Game extends Controller
{
	/**
	 * 用户登录表
	 * @var string
	 */
	public $tbl_login = 'user_login';
	
	/**
	 * 控制初始化方法
	 */
	public function initialize() 
	{
		Core::loadLang('game');
	}
	
	/**
	 * (non-PHPdoc)
	 * @see Controller::index()
	 */
	public function index() 
	{
		$this->list = Game::getGameList();
	}
	
	/**
	 * 游戏入口
	 */
	public function entry()
	{
		if (!User::isLogined()) {
			$this->error('请先登录后再进入游戏。', '/index.php');
		}
		if (!isset($_GET['gid']) || (isset($_GET['gid']) && !ctype_digit($_GET['gid']))) {
			$this->error('未知的游戏。', '/index.php');
		}
		if (!isset($_GET['sid']) || (isset($_GET['sid']) && !ctype_digit($_GET['sid']))) {
			$this->error('未知的游戏服务器。', '/index.php');
		}
		$gameId = $_GET['gid'];
		$gameDetail = Game::read($gameId);
		if (!$gameDetail) {
			$this->error('未知的游戏。', '/index.php');
		}
		$serverId = $_GET['sid'];
		$serverDetail = Game::readServer($serverId);
		if (!$serverDetail) {
			$this->error('未知的游戏服务器。', '/index.php');
		}
		// 用户的一些信息
		$user = User::read($_SESSION['uid'], 'username,from_channel_id,fcmstatus');
		$username = $user['username'];
		$source = $user['from_channel_id'];
	
		$this->gameDetail = $gameDetail;
		$this->serverDetail = $serverDetail;
	
		$gameSign = $gameDetail['sign'];
		// 游戏联合运营的密钥
		$secretKey = Game::$secretKey[$gameSign];
		// 当前时间戳
		$time = time();
		
		// 服务器地址
		$server_url = 'http://'.$serverDetail['domain'];
		
		switch ($gameSign)
		{
			// 神仙道
			case 'sxd':
				$hash = md5($username.'_'.$time.'_'.$secretKey);
				$loginUrl = $server_url.'/login_api.php?user='.$username.'&time='.$time.'&hash='.$hash;
				if ($source) {
					$loginUrl .= '&source='.$source;
				}
			break;
	
			// 盛世三国
			case 'sssg':
				$sign = md5($username.'_'.$time.'_'.$secretKey);
				$loginUrl = $server_url.'/api/login?user='.$username.'&time='.$time.'&sign='.$sign;
				if ($source) {
					$loginUrl .= '&source='.$source;
				}
			break;
	
			// 一骑当先
			case 'yjdx':
				// 0 表示未认证未成年， 1表示未认证成年，2表示认证的未成年，3表示为认证的成年
				if ($user['fcmstatus'] == 1) {
					$adultFlag = 3;
				} else if ($user['fcmstatus'] == 2) {
					$adultFlag = 2;
				} else {
					$adultFlag = 0;
				}
				$tid = Game::$apiTid[$gameSign];
				$sid = $serverInfo['sid'];
				$auth = 'tid='.$tid.'&sid='.$sid.'&account='.$username.'&pwd='.md5(substr($username,0,3))
						.'&ip='.$_SERVER['REMOTE_ADDR'].'&time='.$time.'&adultFlag='.$adultFlag;
				$auth = base64_encode($auth);
				$verify = md5($auth.$secretKey);
				$loginUrl = 'http://up.9787.com/api/1/uinterface.php?action=login&auth='
							.$auth.'&verify='.$verify;
			break;
	
			// 龙将
			case 'lj':
				$sid = $serverInfo['sid'];
				$isAdult = '-1';
				$agent = '591yo';
				$flag = md5($uid.$time.$secretKey);
				$loginUrl = $server_url.'/login.php?username='.$username
							.'&agent='.$agent.'&server='.$sid.'&time='.$time
							.'&isAdult='.$isAdult.'&flag='.$flag;
			break;
	
			// 梦幻飞仙
			case 'mhfx':
				$fm = $user['fcmstatus'];
				$sign = md5($username.$time.$fm.$secretKey);
				$loginUrl = $server_url.'/api/cmn/login.php?account='.$username
							.'&time='.$time.'&fm='.$fm.'&sign='.$sign;
			break;
	
			default:
				;
			break;
		}
	
		$this->loginUrl = $loginUrl;
	
		// 判断是否添加激活游戏服务器
		$value = $_SESSION['uid'].','.$gameId.','.$serverId;
		$key = 'uags:'.$value;
		$status = Cache::get($key);
		if ($status === '1') {
			// 状态重新保留12个小时，
			Cache::set($key, $status, 43200);
			// 当前服务器角色是否已创建
			$isActive = true;
		} else {// 已经缓存，待处理
			$isActive = Game::isRoleExist($username, $serverDetail, $gameDetail);
			if ($isActive) {
				Cache::set($key, 1, 43200);
			} else {
				$mo = new Model('user_active_game_server');
				$data = array(
					'uid'		=> $_SESSION['uid'],
					'game_id'	=> $gameId,
					'server_id'	=> $serverId,
				);
				$exist = $mo->where($data)->fetch();
				if ($exist) {
					Cache::set($key, $exist['status'], 43200);
				} else {
					$data['status'] = 0;
					$list = $mo->add($data);
				}
				Cache::rpush('uags-list', $value);
				$isActive = false;
			}
		}
		
		// js定时检测当前服角色是否已创建，当确认已创建后不再检测
		// 判断规则，每次检测间隔时间加长，只到达到上限后固定间隔
	
	}
	
	/**
	 * 获取指定游戏的所有服务器列表
	 */
	public function getServerList() 
	{
		if (!isset($_GET['game_id']) || !ctype_digit($_GET['game_id'])) {
			$this->json(Core::getLang('game_id_invalid'), 0);
		}
		$list = Game::getServerList($_GET['game_id']);
		
		$this->json('', 1, $list);
	}
	
}